#include "WindowHelper.h"
#include "listeners/CreateServerButtonListener.h"
#include "listeners/ConnectButtonListener.h"
#include "../SDLtoolkit/widgets/EditScenarioPanel.h"
#include "../SDLtoolkit/widgets/PlayScenarioPanel.h"

WindowHelper::WindowHelper() {
}

WindowHelper::~WindowHelper() {
}

stringstream WindowHelper::logging;

Window* WindowHelper::loadSceneWindow() {
	Window* loadSceneWindow = new Window(load);

	Textbox* textBox = new Textbox(100, 300, loadSceneWindow);
	((Container*) loadSceneWindow)->add(textBox);

	std::list<string> scenarios;
	scenarios = Persistor::getScenarioNames();

	TextScrollBar* scroll = new TextScrollBar(20, 20, loadSceneWindow,
			scenarios);
	((Container*) loadSceneWindow)->add(scroll);

	MessageLabel* messageLabel = new MessageLabel(
			"Desea borrar el escenario seleccionado?", loadSceneWindow, 20,
			460, new ConfirmDeleteListener(scroll));
	((Container*) loadSceneWindow)->add(messageLabel);

	Button* deleteButton = new Button("Borrar", "button", 300, 300,
			loadSceneWindow, new DeleteButtonListener(messageLabel));
	((Container*) loadSceneWindow)->add(deleteButton);

	Button* openButton = new Button("Abrir", "button", 450, 300,
			loadSceneWindow, new OpenButtonListener(scroll));
	((Container*) loadSceneWindow)->add(openButton);

	Button* createButton = new Button("Crear", "button", 450, 350,
			loadSceneWindow, new CreateButtonListener(scroll, textBox));
	((Container*) loadSceneWindow)->add(createButton);

	Button* quitButton = new Button("Atras", "button", 450, 400,
			loadSceneWindow, new BackButtonListener);
	((Container*) loadSceneWindow)->add(quitButton);

	return loadSceneWindow;

}

Window* WindowHelper::editSceneWindow(std::string param) {
	logging << "Abriendo el escenario " << param;
	Logger::log("WindowHelper", "editSceneWindow", &logging);

	Window* editScenewindow = new Window(edit);

	MessageLabel* messageLabel = new MessageLabel(
			"Los cambios no se guardaran. Desea cancelar?", editScenewindow,
			20, 620, new ConfirmCancelListener());
	((Container*) editScenewindow)->add(messageLabel);

	EditScenarioPanel* panel = new EditScenarioPanel(param, 0, 0,
			editScenewindow);
	((Container*) editScenewindow)->add(panel);

	Label* label = new Label("Imagen de fondo:", editScenewindow, 600, 600);
	((Container*) editScenewindow)->add(label);

	Textbox* textBox = new Textbox(850, 600, editScenewindow);
	((Container*) editScenewindow)->add(textBox);

	Button* updateButton = new Button("Actualizar", "button", 660, 650,
			editScenewindow, new UpdateButtonListener(textBox, panel));
	((Container*) editScenewindow)->add(updateButton);

	Button* saveButton = new Button("Guardar", "button", 780, 650,
			editScenewindow, new SaveButtonListener(panel));
	((Container*) editScenewindow)->add(saveButton);

	Button* cancelButton = new Button("Cancelar", "button", 900, 650,
			editScenewindow, new CancelButtonListener());
	((Container*) editScenewindow)->add(cancelButton);

	return editScenewindow;

}

Window* WindowHelper::playSceneWindow(BattleMasterInterface* game) {
	logging << "Abriendo el escenario ";
	Logger::log("WindowHelper", "playWindow", &logging);

	Window* playWindow = new Window(play);

	PlayScenarioPanel* panel = new PlayScenarioPanel(10, 10, playWindow, game);
	playWindow->setScenarioContainer(panel);

	Button* quitButton = new Button("Salir", "button", 900, 650, playWindow,
			new ExitButtonListener());
	((Container*) playWindow)->add(quitButton);

	return playWindow;
}

Window* WindowHelper::startGameWindow() {
	logging << "Abriendo ventana";
	Logger::log("WindowHelper", "startGameWindow", &logging);

	Window* startGameWindow = new Window(PRINCIPAL);

	Button* editButton = new Button("Editar", "button", 200, 200,
			startGameWindow, new EditButtonListener());
	((Container*) startGameWindow)->add(editButton);

	Button* connectionButton = new Button("Conectar", "button", 200, 300,
			startGameWindow, new ConnectionButtonListener());
	((Container*) startGameWindow)->add(connectionButton);

	Button* quitButton = new Button("Salir", "button", 200, 400,
			startGameWindow, new ExitButtonListener());
	((Container*) startGameWindow)->add(quitButton);

	return startGameWindow;

}

Window* WindowHelper::connectGameWindow() {

	Window* connectGameWindow = new Window(CONNECT);

	Label* label = new Label("Conexion:", connectGameWindow, 20, 50);
	((Container*) connectGameWindow)->add(label);

	Textbox* connectTextBox = new Textbox(120, 50, connectGameWindow);
	((Container*) connectGameWindow)->add(connectTextBox);

	Label* label2 = new Label("Jugador:", connectGameWindow, 20, 450);
	((Container*) connectGameWindow)->add(label2);

	Textbox* userNameTextBox = new Textbox(120, 450, connectGameWindow);
	((Container*) connectGameWindow)->add(userNameTextBox);

	TextScrollBar* scroll = new TextScrollBar(20, 150, connectGameWindow,
			Persistor::getScenarioNames());
	((Container*) connectGameWindow)->add(scroll);

	CreateServerButtonListener* createServerButtonListener =
			new CreateServerButtonListener(connectTextBox, scroll);
	Button* createButton = new Button("Crear", "button", 450, 400,
			connectGameWindow, createServerButtonListener);
	((Container*) connectGameWindow)->add(createButton);

	ConnectButtonListener* connectButtonListener = new ConnectButtonListener(
			userNameTextBox, connectTextBox);
	Button* enterButton = new Button("Entrar", "button", 450, 450,
			connectGameWindow, connectButtonListener);
	((Container*) connectGameWindow)->add(enterButton);

	Button* quitButton = new Button("Atras", "button", 450, 500,
			connectGameWindow, new BackButtonListener);
	((Container*) connectGameWindow)->add(quitButton);

	return connectGameWindow;

}

Window* WindowHelper::exitWindow() {
	Window* exitWindow = new Window(EXIT);

	Label* label = new Label("Saliendo ...", exitWindow, 20, 50);
	((Container*) exitWindow)->add(label);

	return exitWindow;
}
